PromSql的写法详解

您所在的位置:网站首页 普罗米修斯 label PromSql的写法详解

PromSql的写法详解

2023-10-07 01:09| 来源: 网络整理| 查看: 265

一: 介绍

PromSql是 Prometheus的数据库查询语法,可以方便的查找监控指标的值并且做一些数学运算帮助我们能快速和方便的去统计数据以及设置告警阈值,并且Promsql查询的是时序数据库就算查询历史数据都很快,相比zabbix这一点是很有优势的。

二: Promsql官方文档

官网文档

官方示例

Proms告警规则合集参考

链接: https://awesome-prometheus-alerts.grep.to

三: 表达式语言数据类型(Expression language data types)

(1)字符串面值(String literals): 字符串数值,例子如下

"this is a string" 'these are unescaped: \n \\ \t' `these are not unescaped: \n ' " \t`

(2)浮点型数值(Float literals): 也就是我们常说的小数,比如

3.14

(3)矢量选择器(Instant vector selectors)

http_requests_total # 矢量/向量 http_requests_total{job="prometheus",group="canary"} #过滤指定标签的数据

它包含下面几种比较关系

=: Select labels that are exactly equal to the provided string。—— 等于!=: Select labels that are not equal to the provided string.   ——不等于=~: Select labels that regex-match the provided string. ——正则匹配!~: Select labels that do not regex-match the provided string. —— 正则不匹配

例子:

http_requests_total{environment=~"staging|testing|development",method!="GET"} {job=~".*"} # Bad!

本文博客地址: https://blog.csdn.net/knight_zhou/article/details/104422672

(4)时间选择器

s - seconds (秒)m - minutes(分)h - hours(小时)d - days(天)w - weeks(周)y - years(年)

例子:

http_requests_total{job="prometheus"}[5m] # 5分钟的数据 四 PromSql常用函数

(1) 取绝对值(abs函数)

go_gc_duration_seconds{quantile="0"}

(2)向上取整(ceil函数)

ceil(go_gc_duration_seconds{quantile="0"})

(3) 向下取整(floor函数),与ceil函数相反

floor(go_gc_duration_seconds{quantile="0"})

(4) 四舍五入(round函数)

round(go_gc_duration_seconds{quantile="0"})

(5) sum() 函数:求出找到所有value的值

 sum(go_gc_duration_seconds_count)

(6) avg()函数: 求平均值 

avg(go_gc_duration_seconds_count)

(7) irate() 函数:统计平均速率

## 可以加减乘除 100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) * 100)

(8) rate()函数: 统计速率

rate(v range-vector)函数, 输入:范围向量,输出:key: value = 不带有度量指标,且只有标签列表:(last值-first值)/时间差。

rate(mysql_global_status_questions[2m])

注意: irate适合快速变化的计数器(counter),而rate适合缓慢变化的计数器(counter)

(9) by (标签名)

avg(go_gc_duration_seconds_count) by (instance) # Element 只显示by后的标签值

(10) delta 函数

delta(v range-vector)函数,计算一个范围向量v的第一个元素和最后一个元素之间的差值。返回值:key:value=度量指标:差值,下面这个表达式例子,表示 mysql的慢查询5分钟之类的新增数。

delta(mysql_global_status_slow_queries{}[5m])

五: PromSql运算

支持 加(+) 减(-) 乘(*) 除(/)

比如: 计算内存使用率

((node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes) / (node_memory_MemTotal_bytes)) * 100

六: Promsql 写法实例

1. 计算cpu的个数

count (node_cpu_seconds_total) by (instance)

2. 判断负载

node_load5 > 5 补充

如果想匹配多张网卡:

sum by(instance, hostname) (irate(node_network_receive_bytes_total{device=~"eth0|eth3"}[1m]) / 128 / 1024)

UTC时间禁止在某个时间段告警

# 负载在某个时间段(凌晨3点到4点)的告警, node_load5{} and hour() >19 and hour() 5



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3